package com.seio.edc.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;

public class FileHtml {
	
	private static Logger log = Logger.getLogger(FileHtml.class);
	private static int transID = 1000; //流水号
	
	/**
	 * 读取 html
	 * 
	 * @param filePath
	 * @return
	 */
	public static String readHtmlFile(String filePath) {
		StringBuffer sb = new StringBuffer();
		try {
			String encoding = "utf-8";
			File file = new File(filePath);
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到汉子编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String s = bufferedReader.readLine();
				while (s != null) {
					sb.append(s);
					s = bufferedReader.readLine();
				}
				read.close();
			} else {
				log.error("找不到指定的文件filePath:" + filePath);
				return null;
			}
		} catch (Exception e) {
			log.error("读取文件内容出错filePath:" + filePath + e);
			return null;
		}
		return sb.toString();
	}
	
	/**
	 * 删除单个文件
	 * 
	 * @param sPath 被删除文件的文件名
	 * @return 单个文件删除成功返回true，否则返回false
	 */
	public static boolean deleteFile(String sPath) {
		boolean flag = false;
		File file = new File(sPath);
		// 路径为文件且不为空则进行删除
		if (file.isFile() && file.exists()) {
			file.delete();
			flag = true;
		}
		return flag;
	}  
	
	
	public static synchronized int getTransID() {
		transID ++;
		if (transID == 9999) {
			transID = 1000;
		}
		return transID;
	}
}
